﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AGameOfThrones
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<Window> ModeLessWindows = new List<Window>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            board.Map.InitMap();

            game.game g = FindResource("gamestate") as game.game;
            if (g != null)
            {
                g.Initialize();
            }

            ui.state ui = FindResource("uistate") as ui.state;
            if (ui != null)
            {
                ui.UserHouse = "Stark";
            }

            network.server s = FindResource("server") as network.server;
            if (s != null)
            {
                s.Start();
            }

            network.client c = FindResource("client") as network.client;
            if (c != null)
            {
                c.OpenConnection("127.0.0.1", s.Port);
            }

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            for (int i = 0; i < ModeLessWindows.Count; i++)
            {
                ModeLessWindows[i].Close();
                ModeLessWindows[i] = null;
            }
            ModeLessWindows.Clear();

            network.client c = FindResource("client") as network.client;
            if (c != null)
            {
                c.CloseConnection();
            }

            network.server s = FindResource("server") as network.server;
            if (s != null)
            {
                s.Shutdown();
            }

        }
        
        private void SinglePlayerDebugWindow_Click(object sender, RoutedEventArgs e)
        {
            // Create a window
            common.singleplayer window = new common.singleplayer();

            // Open a window
            window.Show();

            // keep track of window
            ModeLessWindows.Add(window);
        }

        private void SaveAs_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".xml";
            dlg.Filter = "Text documents (.xml)|*.xml";
            if (dlg.ShowDialog() == true)
            {
                string filename = dlg.FileName;
                game.game g = Application.Current.FindResource("gamestate") as game.game;
                stream.ostream_xml s = new stream.ostream_xml();
                s.open(filename);
                g.serialize(s);
                s.close();
            } 
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".xml";
            dlg.Filter = "Text documents (.xml)|*.xml";
            if (dlg.ShowDialog() == true)
            {
                string filename = dlg.FileName;
                game.game g = Application.Current.FindResource("gamestate") as game.game;
                game.factory f = new game.factory();
                stream.istream_xml s = new stream.istream_xml(f);
                s.open(filename);
                g.serialize(s);
                s.close();
            } 
        }

        private void SinglePlayer_Click(object sender, RoutedEventArgs e)
        {

        }

        private void MultiPlayer_Click(object sender, RoutedEventArgs e)
        {


        }

        private void Network_Connect_Click(object sender, RoutedEventArgs e)
        {
            // Create a window
            //common.singleplayer window = new common.singleplayer();

            // Open a window
            //window.Show();

            // keep track of window
            //  ModeLessWindows.Add(window);
        }
    }
}
